<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>第七节：SQLAlchemy的ORM（5） | Flask进阶课程课件</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-si-jie-ff1aflask-sqlalchemy.html" />
    
    
    <link rel="prev" href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-liu-jie-ff1a-sqlalchemy-de-orm-4.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="6.7"
        data-chapter-title="第七节：SQLAlchemy的ORM（5）"
        data-filepath="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-qi-jie-ff1a-sqlalchemy-de-orm-5.md"
        data-basepath=".."
        data-revision="Tue Aug 14 2018 17:50:50 GMT+0800 (中国标准时间)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Introduction
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter1.html">
            
                
                    <a href="../chapter1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        第一章：学前准备
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="chapter1/di-yi-jie-ff1a-xu-ni-huan-jing.html">
            
                
                    <a href="../chapter1/di-yi-jie-ff1a-xu-ni-huan-jing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        第一节：虚拟环境
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="di-er-zhang-ff1a-ren-shi-web.html">
            
                
                    <a href="../di-er-zhang-ff1a-ren-shi-web.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        第二章：认识web
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="di-san-zhang-ff1a-flask-ru-men-2014-2014-url.html">
            
                
                    <a href="../di-san-zhang-ff1a-flask-ru-men-2014-2014-url.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        第三章：URL与视图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-yi-jie-ff1a-di-yi-ge-flask-cheng-xu.html">
            
                
                    <a href="../di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-yi-jie-ff1a-di-yi-ge-flask-cheng-xu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        第一节：Flask简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-er-jie-ff1a-xiang-mu-pei-zhi.html">
            
                
                    <a href="../di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-er-jie-ff1a-xiang-mu-pei-zhi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        第二节：项目配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-san-jie-ff1a-url-yu-shi-tu-han-shu.html">
            
                
                    <a href="../di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-san-jie-ff1a-url-yu-shi-tu-han-shu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        第三节：URL与视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-si-jie-ff1a-guan-yu-xiang-ying.html">
            
                
                    <a href="../di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-si-jie-ff1a-guan-yu-xiang-ying.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        第四节：关于响应
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        第四章：模版
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-yi-jie-ff1a-mo-ban-jian-jie.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-yi-jie-ff1a-mo-ban-jian-jie.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        第一节：模版简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-er-jie-ff1a-jinja2-mo-ban-gai-shu.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-er-jie-ff1a-jinja2-mo-ban-gai-shu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        第二节：Jinja2模版概述
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-san-jie-ff1a-jinja2-guo-lv-qi.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-san-jie-ff1a-jinja2-guo-lv-qi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        第三节：过滤器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-si-jie-ff1a-jinja2-mo-ban-kong-zhi-yu-ju.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-si-jie-ff1a-jinja2-mo-ban-kong-zhi-yu-ju.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        第四节：控制语句
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-wu-jie-ff1a-jinja2-mo-ban-ce-shi-qi.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-wu-jie-ff1a-jinja2-mo-ban-ce-shi-qi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        第五节：测试器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-liu-jie-ff1a-hong-he-import-yu-ju.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-liu-jie-ff1a-hong-he-import-yu-ju.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        第六节：宏和import语句
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-qijie-ff1a-include-he-set-yu-ju.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-qijie-ff1a-include-he-set-yu-ju.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.7.</b>
                        
                        第七节：include和set语句
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.8" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-ba-jie-ff1a-mo-ban-ji-cheng.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-ba-jie-ff1a-mo-ban-ji-cheng.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.8.</b>
                        
                        第八节：模版继承
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-jiu-jie-ff1a-zhuan-yi.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-jiu-jie-ff1a-zhuan-yi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.</b>
                        
                        第九节：转义
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.10" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-shi-jie-ff1a-shu-ju-lei-xing-he-yun-suan-fu.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-shi-jie-ff1a-shu-ju-lei-xing-he-yun-suan-fu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.10.</b>
                        
                        第十节：数据类型和运算符
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.11" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-shi-yi-jie-ff1a-jing-tai-wen-jian-de-pei-zhi.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-shi-yi-jie-ff1a-jing-tai-wen-jian-de-pei-zhi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.11.</b>
                        
                        第十一节：静态文件的配置
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="di-wu-zhang-ff1a-shi-tu-gao-ji.html">
            
                
                    <a href="../di-wu-zhang-ff1a-shi-tu-gao-ji.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        第五章：视图高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="di-wu-zhang-ff1a-shi-tu-gao-ji/di-yi-jie-ff1a-lei-shi-tu.html">
            
                
                    <a href="../di-wu-zhang-ff1a-shi-tu-gao-ji/di-yi-jie-ff1a-lei-shi-tu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        第一节：类视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="di-wu-zhang-ff1a-shi-tu-gao-ji/di-er-jie-ff1a-lan-tu.html">
            
                
                    <a href="../di-wu-zhang-ff1a-shi-tu-gao-ji/di-er-jie-ff1a-lan-tu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        第二节：蓝图和子域名
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku.html">
            
                
                    <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        第六章：SQLAlchemy数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-yi-jie-ff1a-mysql-shu-ju-ku-de-an-zhuang.html">
            
                
                    <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-yi-jie-ff1a-mysql-shu-ju-ku-de-an-zhuang.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        第一节：MySQL数据库的安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-yi-jie-ff1a-sqlalchemy-jie-shao-he-ji-ben-shi-yong.html">
            
                
                    <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-yi-jie-ff1a-sqlalchemy-jie-shao-he-ji-ben-shi-yong.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        第二节：SQLAlchemy介绍和基本使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-san-jie-ff1a-sqlalchemy-de-orm.html">
            
                
                    <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-san-jie-ff1a-sqlalchemy-de-orm.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        第三节：SQLAlchemy的ORM（1）
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-si-jie-ff1a-sqlalchemy-de-orm-2.html">
            
                
                    <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-si-jie-ff1a-sqlalchemy-de-orm-2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        第四节：SQLAlchemy的ORM（2）
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-wu-jie-ff1a-sqlalchemy-de-orm-3.html">
            
                
                    <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-wu-jie-ff1a-sqlalchemy-de-orm-3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        第五节：SQLAlchemy的ORM（3）
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-liu-jie-ff1a-sqlalchemy-de-orm-4.html">
            
                
                    <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-liu-jie-ff1a-sqlalchemy-de-orm-4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        第六节：SQLAlchemy的ORM（4）
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="6.7" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-qi-jie-ff1a-sqlalchemy-de-orm-5.html">
            
                
                    <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-qi-jie-ff1a-sqlalchemy-de-orm-5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.7.</b>
                        
                        第七节：SQLAlchemy的ORM（5）
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.8" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-si-jie-ff1aflask-sqlalchemy.html">
            
                
                    <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-si-jie-ff1aflask-sqlalchemy.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.8.</b>
                        
                        第八节：Flask-SQLAlchemy
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="di-qi-zhang-ff1a-flask-script.html">
            
                
                    <a href="../di-qi-zhang-ff1a-flask-script.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        第七章：Flask-Script
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="di-bazhang-ff1a-alembic-jiao-cheng.html">
            
                
                    <a href="../di-bazhang-ff1a-alembic-jiao-cheng.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        第八章：alembic教程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9" data-path="di-ba-zhang-ff1a-flask-migrate.html">
            
                
                    <a href="../di-ba-zhang-ff1a-flask-migrate.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        第九章：Flask-Migrate
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10" data-path="di-jiu-zhang-ff1a-flask-wtf.html">
            
                
                    <a href="../di-jiu-zhang-ff1a-flask-wtf.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                        第十章：Flask-WTF
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="11" data-path="di-shi-yi-zhang-ff1a-cookie-he-session.html">
            
                
                    <a href="../di-shi-yi-zhang-ff1a-cookie-he-session.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>11.</b>
                        
                        第十一章：cookie和session
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12" data-path="di-shi-zhang-ff1a-shang-xia-wen.html">
            
                
                    <a href="../di-shi-zhang-ff1a-shang-xia-wen.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.</b>
                        
                        第十二章：Flask上下文
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="13" data-path="di-shi-yi-zhang-ff1a-flask-xin-hao-ji-zhi.html">
            
                
                    <a href="../di-shi-yi-zhang-ff1a-flask-xin-hao-ji-zhi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>13.</b>
                        
                        第十三章：Flask信号机制
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="14" data-path="di-shi-si-zhang-ff1a-flask.html">
            
                
                    <a href="../di-shi-si-zhang-ff1a-flask.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>14.</b>
                        
                        第十四章：Flask-Restful
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="14.1" data-path="di-shi-si-zhang-ff1a-flask/di-yi-jie-ff1a-restful-api-gui-fan.html">
            
                
                    <a href="../di-shi-si-zhang-ff1a-flask/di-yi-jie-ff1a-restful-api-gui-fan.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>14.1.</b>
                        
                        第一节：Restful API规范
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="14.2" data-path="di-shi-si-zhang-ff1a-flask/di-er-jie-ff1a-flask-restful-cha-jian.html">
            
                
                    <a href="../di-shi-si-zhang-ff1a-flask/di-er-jie-ff1a-flask-restful-cha-jian.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>14.2.</b>
                        
                        第二节：Flask-Restful插件
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="15" data-path="di-shi-er-zhang-ff1a-memcached-jiao-cheng.html">
            
                
                    <a href="../di-shi-er-zhang-ff1a-memcached-jiao-cheng.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>15.</b>
                        
                        第十五章：memcached教程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="16" data-path="di-shi-sizhang-ff1a-redis-jiao-cheng.html">
            
                
                    <a href="../di-shi-sizhang-ff1a-redis-jiao-cheng.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>16.</b>
                        
                        第十六章：redis教程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="17" data-path="di-shi-liu-zhang-ff1a-csrf-gong-ji.html">
            
                
                    <a href="../di-shi-liu-zhang-ff1a-csrf-gong-ji.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>17.</b>
                        
                        第十七章：CSRF攻击
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="18" data-path="di-shi-ba-zhang-ff1a-qi-niu-yun-pei-zhi.html">
            
                
                    <a href="../di-shi-ba-zhang-ff1a-qi-niu-yun-pei-zhi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>18.</b>
                        
                        第十八章：七牛云配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="19" data-path="di-shi-jiu-zhang-ff1a-bu-shu-flask-xiang-mu.html">
            
                
                    <a href="../di-shi-jiu-zhang-ff1a-bu-shu-flask-xiang-mu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>19.</b>
                        
                        第十九章：部署Flask项目
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >Flask进阶课程课件</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="sqlalchemy&#x7684;orm&#xFF08;6&#xFF09;">SQLAlchemy&#x7684;ORM&#xFF08;6&#xFF09;</h1>
<h3 id="orm&#x5C42;&#x9762;&#x7684;cascade&#xFF1A;">ORM&#x5C42;&#x9762;&#x7684;CASCADE&#xFF1A;</h3>
<p>&#x5982;&#x679C;&#x5C06;&#x6570;&#x636E;&#x5E93;&#x7684;&#x5916;&#x952E;&#x8BBE;&#x7F6E;&#x4E3A;<code>RESTRICT</code>&#xFF0C;&#x90A3;&#x4E48;&#x5728;<code>ORM</code>&#x5C42;&#x9762;&#xFF0C;&#x5220;&#x9664;&#x4E86;&#x7236;&#x8868;&#x4E2D;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x90A3;&#x4E48;&#x4ECE;&#x8868;&#x4E2D;&#x7684;&#x6570;&#x636E;&#x5C06;&#x4F1A;<code>NULL</code>&#x3002;&#x5982;&#x679C;&#x4E0D;&#x60F3;&#x8981;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x53D1;&#x751F;&#xFF0C;&#x90A3;&#x4E48;&#x5E94;&#x8BE5;&#x5C06;&#x8FD9;&#x4E2A;&#x503C;&#x7684;<code>nullable=False</code>&#x3002;</p>
<p>&#x5728;<code>SQLAlchemy</code>&#xFF0C;&#x53EA;&#x8981;&#x5C06;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x6DFB;&#x52A0;&#x5230;<code>session</code>&#x4E2D;&#xFF0C;&#x548C;&#x4ED6;&#x76F8;&#x5173;&#x8054;&#x7684;&#x6570;&#x636E;&#x90FD;&#x53EF;&#x4EE5;&#x4E00;&#x8D77;&#x5B58;&#x5165;&#x5230;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x4E86;&#x3002;&#x8FD9;&#x4E9B;&#x662F;&#x600E;&#x4E48;&#x8BBE;&#x7F6E;&#x7684;&#x5462;&#xFF1F;&#x5176;&#x5B9E;&#x662F;&#x901A;&#x8FC7;<code>relationship</code>&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x6709;&#x4E00;&#x4E2A;&#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;<code>cascade</code>&#x53EF;&#x4EE5;&#x8BBE;&#x7F6E;&#x8FD9;&#x4E9B;&#x5C5E;&#x6027;&#xFF1A;  </p>
<ol>
<li><code>save-update</code>&#xFF1A;&#x9ED8;&#x8BA4;&#x9009;&#x9879;&#x3002;&#x5728;&#x6DFB;&#x52A0;&#x4E00;&#x6761;&#x6570;&#x636E;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4F1A;&#x628A;&#x5176;&#x4ED6;&#x548C;&#x4ED6;&#x76F8;&#x5173;&#x8054;&#x7684;&#x6570;&#x636E;&#x90FD;&#x6DFB;&#x52A0;&#x5230;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x3002;&#x8FD9;&#x79CD;&#x884C;&#x4E3A;&#x5C31;&#x662F;<code>save-update</code>&#x5C5E;&#x6027;&#x5F71;&#x54CD;&#x7684;&#x3002;  </li>
<li><code>delete</code>&#xFF1A;&#x8868;&#x793A;&#x5F53;&#x5220;&#x9664;&#x67D0;&#x4E00;&#x4E2A;&#x6A21;&#x578B;&#x4E2D;&#x7684;&#x6570;&#x636E;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x662F;&#x5426;&#x4E5F;&#x5220;&#x6389;&#x4F7F;&#x7528;<code>relationship</code>&#x548C;&#x4ED6;&#x5173;&#x8054;&#x7684;&#x6570;&#x636E;&#x3002;  </li>
<li><code>delete-orphan</code>&#xFF1A;&#x8868;&#x793A;&#x5F53;&#x5BF9;&#x4E00;&#x4E2A;ORM&#x5BF9;&#x8C61;&#x89E3;&#x9664;&#x4E86;&#x7236;&#x8868;&#x4E2D;&#x7684;&#x5173;&#x8054;&#x5BF9;&#x8C61;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x81EA;&#x5DF1;&#x4FBF;&#x4F1A;&#x88AB;&#x5220;&#x9664;&#x6389;&#x3002;&#x5F53;&#x7136;&#x5982;&#x679C;&#x7236;&#x8868;&#x4E2D;&#x7684;&#x6570;&#x636E;&#x88AB;&#x5220;&#x9664;&#xFF0C;&#x81EA;&#x5DF1;&#x4E5F;&#x4F1A;&#x88AB;&#x5220;&#x9664;&#x3002;&#x8FD9;&#x4E2A;&#x9009;&#x9879;&#x53EA;&#x80FD;&#x7528;&#x5728;&#x4E00;&#x5BF9;&#x591A;&#x4E0A;&#xFF0C;&#x4E0D;&#x80FD;&#x7528;&#x5728;&#x591A;&#x5BF9;&#x591A;&#x4EE5;&#x53CA;&#x591A;&#x5BF9;&#x4E00;&#x4E0A;&#x3002;&#x5E76;&#x4E14;&#x8FD8;&#x9700;&#x8981;&#x5728;&#x5B50;&#x6A21;&#x578B;&#x4E2D;&#x7684;<code>relationship</code>&#x4E2D;&#xFF0C;&#x589E;&#x52A0;&#x4E00;&#x4E2A;<code>single_parent=True</code>&#x7684;&#x53C2;&#x6570;&#x3002;  </li>
<li><code>merge</code>&#xFF1A;&#x9ED8;&#x8BA4;&#x9009;&#x9879;&#x3002;&#x5F53;&#x5728;&#x4F7F;&#x7528;<code>session.merge</code>&#xFF0C;&#x5408;&#x5E76;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4F1A;&#x5C06;&#x4F7F;&#x7528;&#x4E86;<code>relationship</code>&#x76F8;&#x5173;&#x8054;&#x7684;&#x5BF9;&#x8C61;&#x4E5F;&#x8FDB;&#x884C;<code>merge</code>&#x64CD;&#x4F5C;&#x3002;  </li>
<li><code>expunge</code>&#xFF1A;&#x79FB;&#x9664;&#x64CD;&#x4F5C;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4F1A;&#x5C06;&#x76F8;&#x5173;&#x8054;&#x7684;&#x5BF9;&#x8C61;&#x4E5F;&#x8FDB;&#x884C;&#x79FB;&#x9664;&#x3002;&#x8FD9;&#x4E2A;&#x64CD;&#x4F5C;&#x53EA;&#x662F;&#x4ECE;session&#x4E2D;&#x79FB;&#x9664;&#xFF0C;&#x5E76;&#x4E0D;&#x4F1A;&#x771F;&#x6B63;&#x7684;&#x4ECE;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x5220;&#x9664;&#x3002;  </li>
<li><code>all</code>&#xFF1A;&#x662F;&#x5BF9;<code>save-update, merge, refresh-expire, expunge, delete</code>&#x51E0;&#x79CD;&#x7684;&#x7F29;&#x5199;&#x3002;</li>
</ol>
<h3 id="&#x6392;&#x5E8F;&#xFF1A;">&#x6392;&#x5E8F;&#xFF1A;</h3>
<ol>
<li>order_by&#xFF1A;&#x53EF;&#x4EE5;&#x6307;&#x5B9A;&#x6839;&#x636E;&#x8FD9;&#x4E2A;&#x8868;&#x4E2D;&#x7684;&#x67D0;&#x4E2A;&#x5B57;&#x6BB5;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#xFF0C;&#x5982;&#x679C;&#x5728;&#x524D;&#x9762;&#x52A0;&#x4E86;&#x4E00;&#x4E2A;<code>-</code>&#xFF0C;&#x4EE3;&#x8868;&#x7684;&#x662F;&#x964D;&#x5E8F;&#x6392;&#x5E8F;&#x3002;</li>
<li><p>&#x5728;&#x6A21;&#x578B;&#x5B9A;&#x4E49;&#x7684;&#x65F6;&#x5019;&#x6307;&#x5B9A;&#x9ED8;&#x8BA4;&#x6392;&#x5E8F;&#xFF1A;&#x6709;&#x4E9B;&#x65F6;&#x5019;&#xFF0C;&#x4E0D;&#x60F3;&#x6BCF;&#x6B21;&#x5728;&#x67E5;&#x8BE2;&#x7684;&#x65F6;&#x5019;&#x90FD;&#x6307;&#x5B9A;&#x6392;&#x5E8F;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x5B9A;&#x4E49;&#x6A21;&#x578B;&#x7684;&#x65F6;&#x5019;&#x5C31;&#x6307;&#x5B9A;&#x6392;&#x5E8F;&#x7684;&#x65B9;&#x5F0F;&#x3002;&#x6709;&#x4EE5;&#x4E0B;&#x4E24;&#x79CD;&#x65B9;&#x5F0F;&#xFF1A;</p>
<ul>
<li>relationship&#x7684;order_by&#x53C2;&#x6570;&#xFF1A;&#x5728;&#x6307;&#x5B9A;<code>relationship</code>&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4F20;&#x9012;<code>order_by</code>&#x53C2;&#x6570;&#x6765;&#x6307;&#x5B9A;&#x6392;&#x5E8F;&#x7684;&#x5B57;&#x6BB5;&#x3002;</li>
<li><p>&#x5728;&#x6A21;&#x578B;&#x5B9A;&#x4E49;&#x4E2D;&#xFF0C;&#x6DFB;&#x52A0;&#x4EE5;&#x4E0B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code> __mapper_args__ = {
     &quot;order_by&quot;: title
   }
</code></pre><p>&#x5373;&#x53EF;&#x8BA9;&#x6587;&#x7AE0;&#x4F7F;&#x7528;&#x6807;&#x9898;&#x6765;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#x3002;</p>
</li>
</ul>
</li>
<li><p>&#x6B63;&#x5411;&#x6392;&#x5E8F;&#x548C;&#x53CD;&#x5411;&#x6392;&#x5E8F;&#xFF1A;&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x662F;&#x4ECE;&#x5C0F;&#x5230;&#x5927;&#xFF0C;&#x4ECE;&#x524D;&#x5230;&#x540E;&#x6392;&#x5E8F;&#x7684;&#xFF0C;&#x5982;&#x679C;&#x60F3;&#x8981;&#x53CD;&#x5411;&#x6392;&#x5E8F;&#xFF0C;&#x53EF;&#x4EE5;&#x8C03;&#x7528;&#x6392;&#x5E8F;&#x7684;&#x5B57;&#x6BB5;&#x7684;<code>desc</code>&#x65B9;&#x6CD5;&#x3002;</p>
</li>
</ol>
<h3 id="limit&#x3001;offset&#x548C;&#x5207;&#x7247;&#xFF1A;">limit&#x3001;offset&#x548C;&#x5207;&#x7247;&#xFF1A;</h3>
<ol>
<li><code>limit</code>&#xFF1A;&#x53EF;&#x4EE5;&#x9650;&#x5236;&#x6BCF;&#x6B21;&#x67E5;&#x8BE2;&#x7684;&#x65F6;&#x5019;&#x53EA;&#x67E5;&#x8BE2;&#x51E0;&#x6761;&#x6570;&#x636E;&#x3002;</li>
<li><code>offset</code>&#xFF1A;&#x53EF;&#x4EE5;&#x9650;&#x5236;&#x67E5;&#x627E;&#x6570;&#x636E;&#x7684;&#x65F6;&#x5019;&#x8FC7;&#x6EE4;&#x6389;&#x524D;&#x9762;&#x591A;&#x5C11;&#x6761;&#x3002;</li>
<li>&#x5207;&#x7247;&#xFF1A;&#x53EF;&#x4EE5;&#x5BF9;<code>Query</code>&#x5BF9;&#x8C61;&#x4F7F;&#x7528;&#x5207;&#x7247;&#x64CD;&#x4F5C;&#xFF0C;&#x6765;&#x83B7;&#x53D6;&#x60F3;&#x8981;&#x7684;&#x6570;&#x636E;&#x3002;</li>
</ol>
<h3 id="&#x61D2;&#x52A0;&#x8F7D;&#xFF1A;">&#x61D2;&#x52A0;&#x8F7D;&#xFF1A;</h3>
<p>&#x5728;&#x4E00;&#x5BF9;&#x591A;&#xFF0C;&#x6216;&#x8005;&#x591A;&#x5BF9;&#x591A;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5982;&#x679C;&#x60F3;&#x8981;&#x83B7;&#x53D6;&#x591A;&#x7684;&#x8FD9;&#x4E00;&#x90E8;&#x5206;&#x7684;&#x6570;&#x636E;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5F80;&#x5F80;&#x80FD;&#x901A;&#x8FC7;&#x4E00;&#x4E2A;&#x5C5E;&#x6027;&#x5C31;&#x53EF;&#x4EE5;&#x5168;&#x90E8;&#x83B7;&#x53D6;&#x4E86;&#x3002;&#x6BD4;&#x5982;&#x6709;&#x4E00;&#x4E2A;&#x4F5C;&#x8005;&#xFF0C;&#x60F3;&#x8981;&#x6216;&#x8005;&#x8FD9;&#x4E2A;&#x4F5C;&#x8005;&#x7684;&#x6240;&#x6709;&#x6587;&#x7AE0;&#xFF0C;&#x90A3;&#x4E48;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;<code>user.articles</code>&#x5C31;&#x53EF;&#x4EE5;&#x83B7;&#x53D6;&#x6240;&#x6709;&#x7684;&#x3002;&#x4F46;&#x6709;&#x65F6;&#x5019;&#x6211;&#x4EEC;&#x4E0D;&#x60F3;&#x83B7;&#x53D6;&#x6240;&#x6709;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x6BD4;&#x5982;&#x53EA;&#x60F3;&#x83B7;&#x53D6;&#x8FD9;&#x4E2A;&#x4F5C;&#x8005;&#x4ECA;&#x5929;&#x53D1;&#x8868;&#x7684;&#x6587;&#x7AE0;&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x65F6;&#x5019;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x7ED9;<code>relationship</code>&#x4F20;&#x9012;&#x4E00;&#x4E2A;<code>lazy=&apos;dynamic&apos;</code>&#xFF0C;&#x4EE5;&#x540E;&#x901A;&#x8FC7;<code>user.articles</code>&#x83B7;&#x53D6;&#x5230;&#x7684;&#x5C31;&#x4E0D;&#x662F;&#x4E00;&#x4E2A;&#x5217;&#x8868;&#xFF0C;&#x800C;&#x662F;&#x4E00;&#x4E2A;<code>AppendQuery</code>&#x5BF9;&#x8C61;&#x4E86;&#x3002;&#x8FD9;&#x6837;&#x5C31;&#x53EF;&#x4EE5;&#x5BF9;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x518D;&#x8FDB;&#x884C;&#x4E00;&#x5C42;&#x8FC7;&#x6EE4;&#x548C;&#x6392;&#x5E8F;&#x7B49;&#x64CD;&#x4F5C;&#x3002;</p>
<h3 id="&#x67E5;&#x8BE2;&#x9AD8;&#x7EA7;&#xFF1A;">&#x67E5;&#x8BE2;&#x9AD8;&#x7EA7;&#xFF1A;</h3>
<h4 id="groupby&#xFF1A;">group_by&#xFF1A;</h4>
<p>&#x6839;&#x636E;&#x67D0;&#x4E2A;&#x5B57;&#x6BB5;&#x8FDB;&#x884C;&#x5206;&#x7EC4;&#x3002;&#x6BD4;&#x5982;&#x60F3;&#x8981;&#x6839;&#x636E;&#x6027;&#x522B;&#x8FDB;&#x884C;&#x5206;&#x7EC4;&#xFF0C;&#x6765;&#x7EDF;&#x8BA1;&#x6BCF;&#x4E2A;&#x5206;&#x7EC4;&#x5206;&#x522B;&#x6709;&#x591A;&#x5C11;&#x4EBA;&#xFF0C;&#x90A3;&#x4E48;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4EE5;&#x4E0B;&#x4EE3;&#x7801;&#x6765;&#x5B8C;&#x6210;&#xFF1A;</p>
<pre><code class="lang-python">session.query(User.gender,func.count(User.id)).group_by(User.gender).all()
</code></pre>
<h4 id="having&#xFF1A;">having&#xFF1A;</h4>
<p>having&#x662F;&#x5BF9;&#x67E5;&#x627E;&#x7ED3;&#x679C;&#x8FDB;&#x4E00;&#x6B65;&#x8FC7;&#x6EE4;&#x3002;&#x6BD4;&#x5982;&#x53EA;&#x60F3;&#x8981;&#x770B;&#x672A;&#x6210;&#x5E74;&#x4EBA;&#x7684;&#x6570;&#x91CF;&#xFF0C;&#x90A3;&#x4E48;&#x53EF;&#x4EE5;&#x9996;&#x5148;&#x5BF9;&#x5E74;&#x9F84;&#x8FDB;&#x884C;&#x5206;&#x7EC4;&#x7EDF;&#x8BA1;&#x4EBA;&#x6570;&#xFF0C;&#x7136;&#x540E;&#x518D;&#x5BF9;&#x5206;&#x7EC4;&#x8FDB;&#x884C;having&#x8FC7;&#x6EE4;&#x3002;&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-python">result = session.query(User.age,func.count(User.id)).group_by(User.age).having(User.age &gt;= <span class="hljs-number">18</span>).all()
</code></pre>
<h4 id="join&#x65B9;&#x6CD5;&#xFF1A;">join&#x65B9;&#x6CD5;&#xFF1A;</h4>
<p><code>join</code>&#x67E5;&#x8BE2;&#x5206;&#x4E3A;&#x4E24;&#x79CD;&#xFF0C;&#x4E00;&#x79CD;&#x662F;<code>inner join</code>&#xFF0C;&#x53E6;&#x4E00;&#x79CD;&#x662F;<code>outer join</code>&#x3002;&#x9ED8;&#x8BA4;&#x7684;&#x662F;<code>inner join</code>&#xFF0C;&#x5982;&#x679C;&#x6307;&#x5B9A;<code>left join</code>&#x6216;&#x8005;&#x662F;<code>right join</code>&#x5219;&#x4E3A;<code>outer join</code>&#x3002;&#x5982;&#x679C;&#x60F3;&#x8981;&#x67E5;&#x8BE2;<code>User</code>&#x53CA;&#x5176;&#x5BF9;&#x5E94;&#x7684;<code>Address</code>&#xFF0C;&#x5219;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4EE5;&#x4E0B;&#x65B9;&#x5F0F;&#x6765;&#x5B9E;&#x73B0;&#xFF1A;</p>
<pre><code class="lang-python">  <span class="hljs-keyword">for</span> u,a <span class="hljs-keyword">in</span> session.query(User,Address).filter(User.id==Address.user_id).all():
    <span class="hljs-keyword">print</span> u
    <span class="hljs-keyword">print</span> a
  <span class="hljs-comment"># &#x8F93;&#x51FA;&#x7ED3;&#x679C;&#xFF1A;</span>
  &gt; &lt;User (id=<span class="hljs-number">1</span>,name=<span class="hljs-string">&apos;ed&apos;</span>,fullname=<span class="hljs-string">&apos;Ed Jason&apos;</span>,password=<span class="hljs-string">&apos;123456&apos;</span>)&gt;
  &gt; &lt;Address id=<span class="hljs-number">4</span>,email=ed@google.com,user_id=<span class="hljs-number">1</span>&gt;
</code></pre>
<p>&#x8FD9;&#x662F;&#x901A;&#x8FC7;&#x666E;&#x901A;&#x65B9;&#x5F0F;&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;<code>join</code>&#x7684;&#x65B9;&#x5F0F;&#x5B9E;&#x73B0;&#xFF0C;&#x66F4;&#x52A0;&#x7B80;&#x5355;&#xFF1A;</p>
<pre><code class="lang-python">  <span class="hljs-keyword">for</span> u,a <span class="hljs-keyword">in</span> session.query(User,Address).join(Address).all():
    <span class="hljs-keyword">print</span> u
    <span class="hljs-keyword">print</span> a
  <span class="hljs-comment"># &#x8F93;&#x51FA;&#x7ED3;&#x679C;&#xFF1A;</span>
  &gt; &lt;User (id=<span class="hljs-number">1</span>,name=<span class="hljs-string">&apos;ed&apos;</span>,fullname=<span class="hljs-string">&apos;Ed Jason&apos;</span>,password=<span class="hljs-string">&apos;123456&apos;</span>)&gt;
  &gt; &lt;Address id=<span class="hljs-number">4</span>,email=ed@google.com,user_id=<span class="hljs-number">1</span>&gt;
</code></pre>
<p>&#x5F53;&#x7136;&#xFF0C;&#x5982;&#x679C;&#x91C7;&#x7528;<code>outerjoin</code>&#xFF0C;&#x53EF;&#x4EE5;&#x83B7;&#x53D6;&#x6240;&#x6709;<code>user</code>&#xFF0C;&#x800C;&#x4E0D;&#x7528;&#x5728;&#x4E4E;&#x8FD9;&#x4E2A;<code>user</code>&#x662F;&#x5426;&#x6709;<code>address</code>&#x5BF9;&#x8C61;&#xFF0C;&#x5E76;&#x4E14;<code>outerjoin</code>&#x9ED8;&#x8BA4;&#x4E3A;&#x5DE6;&#x5916;&#x67E5;&#x8BE2;&#xFF1A;</p>
<pre><code class="lang-python">  <span class="hljs-keyword">for</span> instance <span class="hljs-keyword">in</span> session.query(User,Address).outerjoin(Address).all():
    <span class="hljs-keyword">print</span> instance
  <span class="hljs-comment"># &#x8F93;&#x51FA;&#x7ED3;&#x679C;&#xFF1A;</span>
  (&lt;User (id=<span class="hljs-number">1</span>,name=<span class="hljs-string">&apos;ed&apos;</span>,fullname=<span class="hljs-string">&apos;Ed Jason&apos;</span>,password=<span class="hljs-string">&apos;123456&apos;</span>)&gt;, &lt;Address id=<span class="hljs-number">4</span>,email=ed@google.com,user_id=<span class="hljs-number">1</span>&gt;)
  (&lt;User (id=<span class="hljs-number">2</span>,name=<span class="hljs-string">&apos;xt&apos;</span>,fullname=<span class="hljs-string">&apos;xiaotuo&apos;</span>,password=<span class="hljs-string">&apos;123&apos;</span>)&gt;, <span class="hljs-keyword">None</span>)
</code></pre>
<h4 id="&#x522B;&#x540D;&#xFF1A;">&#x522B;&#x540D;&#xFF1A;</h4>
<p>&#x5F53;&#x591A;&#x8868;&#x67E5;&#x8BE2;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x6709;&#x65F6;&#x5019;&#x540C;&#x4E00;&#x4E2A;&#x8868;&#x8981;&#x7528;&#x5230;&#x591A;&#x6B21;&#xFF0C;&#x8FD9;&#x65F6;&#x5019;&#x7528;&#x522B;&#x540D;&#x5C31;&#x53EF;&#x4EE5;&#x65B9;&#x4FBF;&#x7684;&#x89E3;&#x51B3;&#x547D;&#x540D;&#x51B2;&#x7A81;&#x7684;&#x95EE;&#x9898;&#x4E86;&#xFF1A;</p>
<pre><code class="lang-python">  <span class="hljs-keyword">from</span> sqlalchemy.orm <span class="hljs-keyword">import</span> aliased
  adalias1 = aliased(Address)
  adalias2 = aliased(Address)
  <span class="hljs-keyword">for</span> username,email1,email2 <span class="hljs-keyword">in</span> session.query(User.name,adalias1.email_address,adalias2.email_address).join(adalias1).join(adalias2).all():
    <span class="hljs-keyword">print</span> username,email1,email2
</code></pre>
<h4 id="&#x5B50;&#x67E5;&#x8BE2;&#xFF1A;">&#x5B50;&#x67E5;&#x8BE2;&#xFF1A;</h4>
<p><code>sqlalchemy</code>&#x4E5F;&#x652F;&#x6301;&#x5B50;&#x67E5;&#x8BE2;&#xFF0C;&#x6BD4;&#x5982;&#x73B0;&#x5728;&#x8981;&#x67E5;&#x627E;&#x4E00;&#x4E2A;&#x7528;&#x6237;&#x7684;&#x7528;&#x6237;&#x540D;&#x4EE5;&#x53CA;&#x8BE5;&#x7528;&#x6237;&#x7684;&#x90AE;&#x7BB1;&#x5730;&#x5740;&#x6570;&#x91CF;&#x3002;&#x8981;&#x6EE1;&#x8DB3;&#x8FD9;&#x4E2A;&#x9700;&#x6C42;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x5B50;&#x67E5;&#x8BE2;&#x4E2D;&#x627E;&#x5230;&#x6240;&#x6709;&#x7528;&#x6237;&#x7684;&#x90AE;&#x7BB1;&#x6570;&#xFF08;&#x901A;&#x8FC7;group by&#x5408;&#x5E76;&#x540C;&#x4E00;&#x7528;&#x6237;&#xFF09;&#xFF0C;&#x7136;&#x540E;&#x518D;&#x5C06;&#x7ED3;&#x679C;&#x653E;&#x5728;&#x7236;&#x67E5;&#x8BE2;&#x4E2D;&#x8FDB;&#x884C;&#x4F7F;&#x7528;&#xFF1A;</p>
<pre><code class="lang-python">  <span class="hljs-keyword">from</span> sqlalchemy.sql <span class="hljs-keyword">import</span> func
  <span class="hljs-comment"># &#x6784;&#x9020;&#x5B50;&#x67E5;&#x8BE2;</span>
  stmt = session.query(Address.user_id.label(<span class="hljs-string">&apos;user_id&apos;</span>),func.count(*).label(<span class="hljs-string">&apos;address_count&apos;</span>)).group_by(Address.user_id).subquery()
  <span class="hljs-comment"># &#x5C06;&#x5B50;&#x67E5;&#x8BE2;&#x653E;&#x5230;&#x7236;&#x67E5;&#x8BE2;&#x4E2D;</span>
  <span class="hljs-keyword">for</span> u,count <span class="hljs-keyword">in</span> session.query(User,stmt.c.address_count).outerjoin(stmt,User.id==stmt.c.user_id).order_by(User.id):
    <span class="hljs-keyword">print</span> u,count
</code></pre>
<p>&#x4ECE;&#x4E0A;&#x9762;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#xFF0C;&#x4E00;&#x4E2A;&#x67E5;&#x8BE2;&#x5982;&#x679C;&#x60F3;&#x8981;&#x53D8;&#x4E3A;&#x5B50;&#x67E5;&#x8BE2;&#xFF0C;&#x5219;&#x662F;&#x901A;&#x8FC7;<code>subquery()</code>&#x65B9;&#x6CD5;&#x5B9E;&#x73B0;&#xFF0C;&#x53D8;&#x6210;&#x5B50;&#x67E5;&#x8BE2;&#x540E;&#xFF0C;&#x901A;&#x8FC7;<code>&#x5B50;&#x67E5;&#x8BE2;.c</code>&#x5C5E;&#x6027;&#x6765;&#x8BBF;&#x95EE;&#x67E5;&#x8BE2;&#x51FA;&#x6765;&#x7684;&#x5217;&#x3002;&#x4EE5;&#x4E0A;&#x65B9;&#x5F0F;&#x53EA;&#x80FD;&#x67E5;&#x8BE2;&#x67D0;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x5177;&#x4F53;&#x5B57;&#x6BB5;&#xFF0C;&#x5982;&#x679C;&#x8981;&#x67E5;&#x627E;&#x6574;&#x4E2A;&#x5B9E;&#x4F53;&#xFF0C;&#x5219;&#x9700;&#x8981;&#x901A;&#x8FC7;<code>aliased</code>&#x65B9;&#x6CD5;&#xFF0C;&#x793A;&#x4F8B;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-python">  stmt = session.query(Address)
  adalias = aliased(Address,stmt)
  <span class="hljs-keyword">for</span> user,address <span class="hljs-keyword">in</span> session.query(User,stmt).join(stmt,User.addresses):
    <span class="hljs-keyword">print</span> user,address
</code></pre>
<h3 id="&#x8001;&#x6BB5;&#x7684;&#x4F5C;&#x4E1A;&#xFF1A;">&#x8001;&#x6BB5;&#x7684;&#x4F5C;&#x4E1A;&#xFF1A;</h3>
<p><a href="http://blog.csdn.net/laoduan_78/article/details/44259245" target="_blank">http://blog.csdn.net/laoduan_78/article/details/44259245</a></p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-liu-jie-ff1a-sqlalchemy-de-orm-4.html" class="navigation navigation-prev " aria-label="Previous page: 第六节：SQLAlchemy的ORM（4）"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-si-jie-ff1aflask-sqlalchemy.html" class="navigation navigation-next " aria-label="Next page: 第八节：Flask-SQLAlchemy"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
